package com.platform.controller;


import com.platform.entity.OrderEntity;
import com.platform.entity.UserEntity;
import com.platform.entity.UserInviteesExportEntity;
import com.platform.service.impl.InviterServiceImpl;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.StringUtils;
import com.platform.utils.excel.ExcelExport;
import com.platform.vo.UserInviteesVo;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 /**@Author chenweigang
 * 邀请人与被邀请人
 */
@RestController
@RequestMapping("invite")
public class InviterController {
    @Autowired
    private InviterServiceImpl inviterService;

    @PostMapping("/list")
    @RequiresPermissions("invite:list")
    public R list(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<UserInviteesVo> orderList = inviterService.queryUserInviteesList(query);
        int total = inviterService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(orderList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/info")
    public R info(@RequestBody Map<String, Object> params) {
        Query query = new Query(params);
        List<OrderEntity> orderEntities = inviterService.queryUserInviteesOrder(query);
        int total = inviterService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(orderEntities, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/export")
    public R export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        String parentIdString=params.get("parentId").toString();
        Integer parentId=null;
        if (StringUtils.isNotEmpty(parentIdString)) {
             parentId=Integer.parseInt(parentIdString);
        }

        List<UserInviteesExportEntity> exportList = inviterService.export(parentId);
        ExcelExport ee = new ExcelExport("邀请人列表");

        String[] header = new String[]{"邀请人姓名", "邀请人电话", "被邀请人姓名",
                "被邀请人姓名","被邀请人下的订单号","订单总价","实际支付","订单状态","订单时间"};

        List<Map<String, Object>> list = new ArrayList<>();

        if (exportList != null && exportList.size() != 0) {
            for (UserInviteesExportEntity exportEntity : exportList) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("parentName", exportEntity.getParentName());
                map.put("parentMobile", exportEntity.getParentMobile());
                map.put("nickName", exportEntity.getNickName());
                map.put("mobile", exportEntity.getMobile());
                map.put("orderSn", exportEntity.getOrderSn());
                map.put("orderPrice", exportEntity.getOrderPrice());
                map.put("actualPrice", exportEntity.getActualPrice());
                map.put("orderStatus", exportEntity.getOrderStatusText());
                map.put("addTime",exportEntity.getAddTime());
                list.add(map);
            }
        }

        ee.addSheetByMap("邀请人列表", list, header);
        ee.export(response);
        return R.ok();
    }
}

